library(tidyverse)
library(haven)
# Define a function to read data from a URL
read_data <- function(df)
{
# Combine the base URL with the file name
full_path <- paste("https://github.com/scunning1975/mixtape/raw/master/",
df, sep = "")
# Read the .dta file from the URL
df <- read_dta(full_path)
# Return the data frame
return(df)
}
# Read the 'titanic.dta' data and create a new variable 'd'
titanic <- read_data("titanic.dta") %>%
mutate(d = case_when(class == 1 ~ 1, TRUE ~ 0)) # 'd' is 1 if 'class' is 1, 0 otherwise
# Calculate the mean of 'survived' for observations where 'd' is 1
ey1 <- titanic %>%
filter(d == 1) %>%
pull(survived) %>%
mean()
# Calculate the mean of 'survived' for observations where 'd' is 0
ey0 <- titanic %>%
filter(d == 0) %>%
pull(survived) %>%
mean()
# Calculate the difference in means
sdo <- ey1 - ey0
# Create a data frame
results <- data.frame(
ey1 = ey1,
ey0 = ey0,
sdo = sdo
)
# Print the data frame
print(results)